﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDCRNewEdit.Model;
using LDCRNewEdit.Services;
using LDCRNewEdit.Utils;

namespace LDCRNewEdit.UI.地图编辑.场景地图设计
{
    public partial class SceneNPCForm : Form
    {
        private SceneNPC npc = null;

        public SceneNPCForm()
        {
            InitializeComponent();

            this.comboBoxPerson.Items.Add("-1");
            foreach (BaseData data in BaseDataService.BaseDataList["6"])
            {
                this.comboBoxPerson.Items.Add(data.Name);
            }

            this.comboBoxPerson.SelectedIndex = 0;
            this.comboBoxEventType.SelectedIndex = 0;
            this.comboBoxDirection.SelectedIndex = 0;
        }

        public SceneNPC ShowDialogEvent(SceneNPC npc)
        {
            this.npc = npc;
           
            this.comboBoxEventType.SelectedIndex = this.npc.EventType;
            this.comboBoxPerson.SelectedIndex = this.npc.EID + 1;
            this.textBoxX.Text = this.npc.X.ToString();
            this.textBoxY.Text = this.npc.Y.ToString();
            this.comboBoxDirection.SelectedIndex = this.npc.Direction;
            this.textBoxVoice.Text = this.npc.Voice.ToString();

            //事件
            foreach (SceneEvent e in this.npc.EventList)
            {
                this.listView1.Items.Add(e.ID);
            }

            

            if (this.ShowDialog() == DialogResult.OK)
            {
                this.Close();
                return this.npc;
            }
            else
            {
                this.Close();
                return null;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            npc.EventType = (byte)this.comboBoxEventType.SelectedIndex;
            npc.X = short.Parse(this.textBoxX.Text);
            npc.Y = short.Parse(this.textBoxY.Text);
            npc.EventList.Clear();
            for (byte i = 0; i < listView1.Items.Count; i++)
            {
                npc.EventList.Add(new SceneEvent(listView1.Items[i].Text, i));
            }

            npc.EID = (short)(this.comboBoxPerson.SelectedIndex - 1);
            npc.Direction = (byte)this.comboBoxDirection.SelectedIndex;
            npc.Voice = short.Parse(this.textBoxVoice.Text);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void comboBoxEventType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.comboBoxEventType.SelectedIndex == 1)
            {
                this.comboBoxPerson.Enabled = false;
                this.comboBoxDirection.Enabled = false;
                this.textBoxVoice.ReadOnly = true;
            }
            else
            {
                this.comboBoxPerson.Enabled = true;
                this.comboBoxDirection.Enabled = true;
                this.textBoxVoice.ReadOnly = false;
            }
        }

        private void textBoxEvent_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.lua|*.lua";
            ofd.InitialDirectory = Resources.GetPath("EventPath");
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBoxEvent.Text = System.IO.Path.GetFileName(ofd.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string value = this.textBoxEvent.Text.Trim();
            if (string.IsNullOrEmpty(value))
            {
                MessageBox.Show("请选择事件文件!");
            }
            else
            {
                this.listView1.Items.Add(value);
                this.textBoxEvent.Text = string.Empty;
            }
        }
    }
}
